package com.spring.service.impl;

import com.spring.respone.ApiRestResponse;
import com.spring.service.GetCodeService;
import com.spring.service.SendJunkMailService;
import com.spring.until.KeyUtil;
import com.spring.until.SMSUtil;
import com.spring.until.SessionUtils;
import com.spring.until.UserPass;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;

@Service
public class GetCodeServiceImpl implements GetCodeService {
    @Autowired
    SendJunkMailService sendJunkMailService ;

    @Override
    public ApiRestResponse getCode(HttpServletRequest request) {
        UserPass userPass = UserPass.getUserPass(request) ;
        int i = 0 ;

        if(userPass.getKind()){
            i = sendMessage(userPass,request) ;
        }else{
            i = sendEmail(userPass,request) ;
        }
        if(i==1){
            return ApiRestResponse.success().setMsg("验证码发送成功").setStatus(1);
        }else{
            return ApiRestResponse.error().setMsg("验证码发送失败").setStatus(0) ;
        }
    }

    public int sendMessage(UserPass userPass , HttpServletRequest request) {
        String code = KeyUtil.keyUtils() ;
        boolean ok = SMSUtil.sendSMS(userPass.getCount(),code) ;
        if(ok){
            SessionUtils.addCode(userPass,code);
            return 1 ;
        }
        return 0 ;
    }

    public int sendEmail(UserPass userPass,HttpServletRequest request) {
        String code = KeyUtil.keyUtils();
        boolean res = sendJunkMailService.sendJunkMail(userPass.getCount(),code);
        if(res){
            SessionUtils.addCode(userPass,code);
            return 1 ;
        }
        return 0 ;
    }
}